package br.unifor.cct.mia.coevolution.evaluate;

import java.io.IOException;

import br.unifor.cct.mia.coevolution.CoevolutionaryDomain;
import br.unifor.cct.mia.coevolution.InvalidTypeException;
import br.unifor.cct.mia.dataenhancement.Database;
import br.unifor.cct.mia.dataenhancement.Structure;
import br.unifor.cct.mia.evaluate.Evaluate;
import br.unifor.cct.mia.evolutionary.Particula;
import br.unifor.cct.mia.pso.PSO;

public class PSOCoevolutionaryEvaluation implements Evaluate {

	public Object evaluate(Object value,String[] options) {
		Object[] o = (Object[]) value;
		CoevolutionaryDomain domain = (CoevolutionaryDomain)o[0];
		Integer specieType = (Integer)o[1];
		Structure struc = (Structure)o[2];
		Database db = (Database)o[3];
		PSO pso = (PSO)o[4];
		
        for(Particula p:pso.getPopulacao()) {        	
        	       	
			try {
				//TODO Nao tem nBits
				domain.evaluate(p,specieType.intValue(),struc,db,0);	
				System.out.println(p); 
				
				double fitness = p.getFitness();
				if ( fitness>p.getPBest().getFitness() ) {
					p.setPBest((Particula)p.clone());
				}
				
				if ( fitness>pso.getBestIndividual().getFitness() ) {
					pso.setBestIndividual((Particula)p.clone());
				}
				
			} catch (IOException e) {				
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (InvalidTypeException e) {				
				e.printStackTrace();
			}   
        }
        
		return pso.getPopulacao();
	}
	
}
